import throttle from "@/uview-ui/libs/function/throttle.js"

export function navigateTo(url) {
	const user = uni.getStorageSync("user");
	if (!user || !user.id) {
		showModal()
		return;
	}
	throttle(() => {
		uni.navigateTo({
			url: url
		})
	}, 1000, true)
}

export function navigateToNoUser(url) {
	throttle(() => {
		uni.navigateTo({
			url: url
		})
	}, 1000, true)
}


export function redirectTo(url) {
	const user = uni.getStorageSync("user");
	if (!user || !user.id) {
		showModal()
		return;
	}
	throttle(() => {
		uni.redirectTo({
			url: url
		})
	}, 1000, true)
}

export function reLaunch(url) {
	const user = uni.getStorageSync("user");
	if (!user || !user.id) {
		showModal()
		return;
	}
	throttle(() => {
		uni.reLaunch({
			url: url
		})
	}, 1000, true)
}

function showModal() {
	uni.showModal({
		title: "提示",
		content: "您当前尚未登录",
		confirmText: "去登录",
		success(res) {
			if (res.confirm) {
				uni.navigateTo({
					url: "/pages/tabBar/login/login"
				})
			}
		}
	})
}